package org.sky.nio;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class NIOClient {
	public static void main(String[] args) throws InterruptedException,
			IOException {
		EventLoopGroup group = new NioEventLoopGroup();
		try {
			Bootstrap b = new Bootstrap();
			b.group(group).channel(NioSocketChannel.class)
					.handler(new ChannelInitializer<SocketChannel>() {

						@Override
						protected void initChannel(SocketChannel ch)
								throws Exception {
							ChannelPipeline pipeline = ch.pipeline();

							/*
							 * 这个地方的 必须和服务端对应上。否则无法正常解码和编码
							 * 
							 * 解码和编码 我将会在下一张为大家详细的讲解。再次暂时不做详细的描述
							 */
							pipeline.addLast("framer", new LineBasedFrameDecoder(256));
							pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
							pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));

							// 客户端的逻辑
							pipeline.addLast("handler",
									new SimpleChannelInboundHandler<String>() {
										@Override
										protected void channelRead0( ChannelHandlerContext ctx,String msg) throws Exception {
											System.out.println("Server say : " + msg);
										}

										@Override
										public void channelActive(ChannelHandlerContext ctx) throws Exception {
											System.out.println("Client active ");
											super.channelActive(ctx);
										}

										@Override
										public void channelInactive(ChannelHandlerContext ctx) throws Exception {
											System.out.println("Client close ");
											super.channelInactive(ctx);
										}
									});
						}

					});

			// 连接服务端
			Channel ch = b.connect("127.0.0.1", 8000).sync().channel();
			// 控制台输入
			BufferedReader in = new BufferedReader(new InputStreamReader(
					System.in));
			for (;;) {
				String line = in.readLine();
				if (line == null) {
					continue;
				}
				/*
				 * 向服务端发送在控制台输入的文本 并用"\r\n"结尾 之所以用\r\n结尾 是因为我们在handler中添加了
				 * DelimiterBasedFrameDecoder 帧解码。
				 * 这个解码器是一个根据\n符号位分隔符的解码器。所以每条消息的最后必须加上\n否则无法识别和解码
				 */
				ch.writeAndFlush(line+"\n");
			}
		} finally {
			// The connection is closed automatically on shutdown.
			group.shutdownGracefully();
		}
	}
}
